package com.wowangz.cms.systemmanage.exportreport.api;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.tagext.TryCatchFinally;

import org.apache.commons.net.ftp.FTPClient;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wowangz.cms.syscore.api.BaseController;
import com.wowangz.cms.syscore.api.JSONFactory;
import com.wowangz.cms.syscore.utils.ServletUtils;
import com.wowangz.cms.systemmanage.exportreport.lucentask.FTPHelper;
import com.wowangz.cms.systemmanage.exportreport.lucentask.SSHHelper;

import net.sf.json.JSONObject;


@Scope("prototype")
@Controller
public class DownVoiceFileController extends BaseController {

	@RequestMapping(value = "/systemmanage/exportreport/download.web")
    public void download(HttpServletRequest request,  
            HttpServletResponse response)  throws Exception {  
		     try {
		    	  HashMap<String, Object> map = (HashMap) ServletUtils.getParametersToHashMap(request);
			        String fname = (String) map.get("fname");
			        String date = (String) map.get("date");
			
			        //fname = fname+".mp4";
			        
			        response.setContentType("text/html;charset=UTF-8");  
			        request.setCharacterEncoding("UTF-8");  
			        response.setContentType("application/octet-stream");  
			        response.setHeader("Content-disposition", "attachment; filename="  
			                + new String(fname));  
			  
			        //SSH通过文件名获取文件的全路径
			        String path = SSHHelper.getOneFilePath(date,fname);
			        
			        //连接FTP""
			        FTPHelper fh = new FTPHelper();
			        
			        if(fh.connectServer()){
			        	FTPClient ftpClient = fh.getFtpClient();
			        	ftpClient.enterLocalPassiveMode(); 
						ftpClient.changeWorkingDirectory(path) ;
						OutputStream os = response.getOutputStream();
		                ftpClient.retrieveFile(fname, os);
		                os.flush();
		                os.close();
		                fh.closeConnect();
			        }
			} catch (Exception e) {
				e.printStackTrace();
			}
		      
    }  
	
	@RequestMapping(value = "/systemmanage/exportreport/mp4exists.web")
    public void mp4exists(HttpServletRequest request,  
            HttpServletResponse response)  throws Exception {  
		
		try {
			 HashMap<String, Object> map = (HashMap) ServletUtils.getParametersToHashMap(request);
		        String fname = (String) map.get("fname");
		        String date = (String) map.get("date");
		  
		        //SSH通过文件名获取文件的全路径
		        String path = SSHHelper.getOneFilePath(date,fname);
		        
		       //如果path为空，那么证明没有录音文件
		        Map<String,String> result = new HashMap<String,String>();
		       if(path==null || "".equals(path)){
		    	   result.put("msg", "0");
		       }else{
		    	   result.put("msg", "1");   
		       }
		       response.getWriter().write(JSONObject.fromObject(result).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
    } 
}
